package com.qf.lanzhou.shop.api.controller;

import com.qf.lanzhou.shop.api.service.PageService;
import com.qf.lanzhou.shop.pojo.entity.Goods;
import com.qf.lanzhou.shop.pojo.entity.Page;
import com.qf.lanzhou.shop.pojo.entity.ShopCart;
import com.qf.lanzhou.shop.pojo.vo.BaseRespVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author 是柠檬甜
 * @date 2021/1/11 14:52
 */
@Controller
public class PageController extends BaseController{
    @Autowired
    private PageService pageService;

    @ApiOperation("分页展示")
    @RequestMapping("/pageShow")
    @ResponseBody
    public BaseRespVO pageShow(Integer pageIndex) {
        if (pageIndex==null){
            pageIndex=1;
        }
        Page page = new Page(Integer.valueOf(pageIndex));
        List<Goods> goods = pageService.pageDivide(page);
        return convertSuccessResult(goods);
    }

    @ApiOperation("分页总数")
    @RequestMapping("/pageCount")
    @ResponseBody
    public BaseRespVO pageCount() {
        int count = pageService.count();
        return convertSuccessResult(count);
    }

    @ApiOperation("搜索分页展示")
    @RequestMapping("/searchPageShow")
    @ResponseBody
    public BaseRespVO searchPageShow(Integer pageIndex,String searchKey) {
        System.out.println("搜索分页展示"+searchKey);
        if (pageIndex==null){
            pageIndex=1;
        }
        Page page = new Page(Integer.valueOf(pageIndex));
        List<Goods> goods = pageService.pageSearch(page,searchKey);
        return convertSuccessResult(goods);
    }

    @ApiOperation("搜索分页总数")
    @RequestMapping("/searchPageCount")
    @ResponseBody
    public BaseRespVO searchPageCount(String searchKey) {
        int count = 0;
        try {
            count = pageService.searchCount(searchKey);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return convertSuccessResult(count);
    }
}
